function TestCommand(){
	this.event;
}
TestCommand.prototype.execute = function ( event ){
	//console.log( event );
	var model = ModelLocator.getInstance();
	model.state.error = event.payload.data;
}

/*#################### LogOutCommand ####################*/
function LogOutCommand(){
}

LogOutCommand.prototype.execute = function ( event ){
	var model = ModelLocator.getInstance();
	//increment asynch count
	model.state.aSynchOperationCount += 1;
	//call dao
	if( event.payload == null || event.payload.data instanceof User != true ){
		throw ("LogOutCommand.execute: Must pass a user");
		return;
	}
	//for some reason JQuery insists on overriding the this keyword. STUPID!!! So I have to pass a refference in event.data.commandInstance
	ServiceLocator.getInstance().dao.deleteUserLocalStore( event.payload.data, event.data.commandInstance );
}

LogOutCommand.prototype.result = function ( event ){
	//console.log( event.result );
	var model = ModelLocator.getInstance();
	//model.testValue = event.payload.data;
	model.state.aSynchOperationCount -= 1;
	if( model.state.aSynchOperationCount == 0 ){
		model.state.aSynchOpComplete = true;
	}
	try{
		model.state.user.fbToken = null;
		model.state.showFBLoginButton = true;
	}catch(e){
		model.state.error = true;
		model.state.errorMassage = e;
	}
}

LogOutCommand.prototype.fault = function ( event ){
	console.log( event.error );
	var model = ModelLocator.getInstance();
	model.state.aSynchOperationCount -= 1;
	if( model.state.aSynchOperationCount == 0 ){
		model.state.aSynchOpComplete = true;
	}
	model.state.appErrorMessage = "An error occurred on the server. " + event.error;
	model.state.appError = true;
}
/*#################### End LogOutCommand ####################*/

/*#################### UpdateUserCommand ####################*/
function UpdateUserCommand(){
}

UpdateUserCommand.prototype.execute = function ( event ){
	var model = ModelLocator.getInstance();
	//increment asynch count
	model.state.aSynchOperationCount += 1;
	//call dao
	if( event.payload == null || event.payload.data instanceof User != true ){
		throw ("UpdateUserCommand.execute: Must pass a user");
		return;
	}
	//for some reason JQuery insists on overriding the this keyword. STUPID!!! So I have to pass a refference in event.data.commandInstance
	ServiceLocator.getInstance().dao.updateUser( event.payload.data, event.data.commandInstance );
}

UpdateUserCommand.prototype.result = function ( event ){
	//console.log( event.result );
	var model = ModelLocator.getInstance();
	//model.testValue = event.payload.data;
	model.state.aSynchOperationCount -= 1;
	if( model.state.aSynchOperationCount == 0 ){
		model.state.aSynchOpComplete = true;
	}
	try{
		//ParserFactory.getInstance( ModelLocator.getInstance().config ).decodeRole( data );
		if( event.result != null ){
			//parse the result
			var userObj = ParserFactory.getInstance( model.config.parser ).decodeUser( event.result );
			//get the roles for the user and store
			model.state.user.roles = userObj.roles;
			//Now that the user is saved server side then save locally
			ServiceLocator.getInstance().dao.localStoreUser( model.state.user );
			model.state.showFBLoginButton = false;
		}
	}catch(e){
		console.log( e );
		model.state.error = true;
		model.state.errorMassage = e;
	}
}

UpdateUserCommand.prototype.fault = function ( event ){
	console.log( event.error.message );
	var model = ModelLocator.getInstance();
	model.state.aSynchOperationCount -= 1;
	if( model.state.aSynchOperationCount == 0 ){
		model.state.aSynchOpComplete = true;
	}
	model.state.appErrorMessage = "An error occurred on the server. " + event.error.message;
	model.state.appError = true;
	if( event.error.type == 'OAuthException' ){
		ServiceLocator.getInstance().dao.deleteUserLocalStore( model.state.user );
		model.state.user.fbToken = null;
		model.state.showFBLoginButton = true;
	}
}
/*#################### End UpdateUserCommand ####################*/

/*#################### GetFBProfileCommand ####################*/
function GetFBProfileCommand(){
}

GetFBProfileCommand.prototype.execute = function ( event ){
	var model = ModelLocator.getInstance();
	//increment asynch count
	model.state.aSynchOperationCount += 1;
	//call dao
	if( event.payload == null || event.payload.data instanceof User != true ){
		throw ("GetFBProfileCommand.execute: Must pass a user");
		return;
	}
	//for some reason JQuery insists on overriding the this keyword. STUPID!!! So I have to pass a refference in event.data.commandInstance
	ServiceLocator.getInstance().dao.getFBProfile( event.payload.data, event.data.commandInstance );
}

GetFBProfileCommand.prototype.result = function ( event ){
	//console.log( event.result );
	var model = ModelLocator.getInstance();
	//model.testValue = event.payload.data;
	model.state.aSynchOperationCount -= 1;
	if( model.state.aSynchOperationCount == 0 ){
		model.state.aSynchOpComplete = true;
	}
	try{
		//ParserFactory.getInstance( ModelLocator.getInstance().config ).decodeRole( data );
		if( event.result != null ){
			//parse the result
			var fbObj = ParserFactory.getInstance( model.config.parser ).decodeFBProfile( event.result );
			//update the model's user with the profile data
			model.state.user.fbID = fbObj.id;
			model.state.user.email = fbObj.email;
			model.state.user.firstName = fbObj.firstName;
			model.state.user.lastName = fbObj.lastName;
			model.state.user.timezone = fbObj.timezone;
			//add the users roles server side for security
			//dispatch event to persist user
			$(eventDispatcher).trigger( new SrfBetEvent( SrfBetEvent.UPDATE_USER, {data:model.state.user} ) );
		}
	}catch(e){
		console.log( e );
		model.state.error = true;
		model.state.errorMassage = e;
	}
}

GetFBProfileCommand.prototype.fault = function ( event ){
	console.log( event.error.message );
	var model = ModelLocator.getInstance();
	model.state.aSynchOperationCount -= 1;
	if( model.state.aSynchOperationCount == 0 ){
		model.state.aSynchOpComplete = true;
	}
	model.state.appErrorMessage = "An error occurred on the server. " + event.error.message;
	model.state.appError = true;
	if( event.error.type == 'OAuthException' ){
		ServiceLocator.getInstance().dao.deleteUserLocalStore( model.state.user );
		model.state.user.fbToken = null;
		model.state.showFBLoginButton = true;
	}
}
/*#################### End GetFBProfileCommand ####################*/

/*#################### UpdateDomainCommand ####################*/
function UpdateDomainCommand(){
}
UpdateDomainCommand.prototype.execute = function ( event ){
	var model = ModelLocator.getInstance();
	//increment asynch count
	model.state.aSynchOperationCount += 1;
	//call dao
	if( event.payload == null || event.payload.data instanceof Domain != true ){
		throw ("UpdateDomainCommand.execute: Must pass a domain");
		return;
	}
	//for some reason JQuery insists on overriding the this keyword. STUPID!!! So I have to pass a refference in event.data.commandInstance
	ServiceLocator.getInstance().dao.updateDomain( event.payload.data, event.data.commandInstance );
}

UpdateDomainCommand.prototype.result = function ( event ){
	//console.log( event.result );
	var model = ModelLocator.getInstance();
	//model.testValue = event.payload.data;
	model.state.aSynchOperationCount -= 1;
	if( model.state.aSynchOperationCount == 0 ){
		model.state.aSynchOpComplete = true;
	}
	try{
		if( event.result != null ){
			//parse the result
			var domain = ParserFactory.getInstance( model.config.parser ).decodeDomain( event.result );
			//add the domain to the model's domains array
			//note, intempotency is a huge issue with SimpleDB so always use accosiative arrays because adds are the same as updates
			model.domains[ domain.name ] = domain;
		}
	}catch(e){
		model.state.error = true;
		model.state.errorMassage = e;
	}
}

UpdateDomainCommand.prototype.fault = function ( event ){
	console.log( event.error );
	var model = ModelLocator.getInstance();
	model.state.aSynchOperationCount -= 1;
	if( model.state.aSynchOperationCount == 0 ){
		model.state.aSynchOpComplete = true;
	}
	model.state.appErrorMessage = "An error occurred on the server. " + event.error;
	model.state.appError = true;
}
/*#################### End UpdateDomainCommand ####################*/

/*#################### UpdateRoleCommand ####################*/
function UpdateRoleCommand(){
}

UpdateRoleCommand.prototype.execute = function ( event ){
	var model = ModelLocator.getInstance();
	//increment asynch count
	model.state.aSynchOperationCount += 1;
	//call dao
	if( event.payload == null || event.payload.data instanceof Role != true ){
		throw ("UpdateRoleCommand.execute: Must pass a role");
		return;
	}
	//for some reason JQuery insists on overriding the this keyword. STUPID!!! So I have to pass a refference in event.data.commandInstance
	ServiceLocator.getInstance().dao.updateRole( event.payload.data, event.data.commandInstance );
}

UpdateRoleCommand.prototype.result = function ( event ){
	//console.log( event.result );
	var model = ModelLocator.getInstance();
	//model.testValue = event.payload.data;
	model.state.aSynchOperationCount -= 1;
	if( model.state.aSynchOperationCount == 0 ){
		model.state.aSynchOpComplete = true;
	}
	try{
		//ParserFactory.getInstance( ModelLocator.getInstance().config ).decodeRole( data );
		if( event.result != null ){
			//parse the result
			var role = ParserFactory.getInstance( model.config.parser ).decodeRole( event.result );
			//add the role to the security array
			//noe, intempotency is a huge issue with SimpleDB so always use accosiative arrays because adds are the same as updates
			model.security.roles[ role.name ] = role;
		}
	}catch(e){
		model.state.error = true;
		model.state.errorMassage = e;
	}
}

UpdateRoleCommand.prototype.fault = function ( event ){
	console.log( event.error );
	var model = ModelLocator.getInstance();
	model.state.aSynchOperationCount -= 1;
	if( model.state.aSynchOperationCount == 0 ){
		model.state.aSynchOpComplete = true;
	}
	model.state.appErrorMessage = "An error occurred on the server. " + event.error;
	model.state.appError = true;
}
/*#################### End UpdateRoleCommand ####################*/

/*#################### LocalSroreUserCommand ####################*/
function LocalSroreUserCommand(){
}

LocalSroreUserCommand.prototype.execute = function ( event ){
	var model = ModelLocator.getInstance();
	//increment asynch count
	model.state.aSynchOperationCount += 1;
	//call dao
	if( event.payload == null || event.payload.data instanceof User != true ){
		throw ("LocalSroreUserCommand.execute: Must pass a user");
		return;
	}
	//for some reason JQuery insists on overriding the this keyword. STUPID!!! So I have to pass a refference in event.data.commandInstance
	ServiceLocator.getInstance().dao.localStoreUser( event.payload.data, event.data.commandInstance );
}

LocalSroreUserCommand.prototype.result = function ( event ){
	//console.log( event );
	var model = ModelLocator.getInstance();
	//model.testValue = event.payload.data;
	model.state.aSynchOperationCount -= 1;
	if( model.state.aSynchOperationCount == 0 ){
		model.state.aSynchOpComplete = true;
	}
	model.state.user = event.user;
}

LocalSroreUserCommand.prototype.fault = function ( event ){
	//console.log( event );
	var model = ModelLocator.getInstance();
	model.state.aSynchOperationCount -= 1;
	if( model.state.aSynchOperationCount == 0 ){
		model.state.aSynchOpComplete = true;
	}
	model.state.appErrorMessage = "An error occurred on the server. " + event.error;
	model.state.appError = true;
}
/*#################### End LocalSroreUserCommand ####################*/

/*#################### LoginUserCommand ####################*/
function LoginUserCommand(){
}

LoginUserCommand.prototype.execute = function ( event ){
	var model = ModelLocator.getInstance();
	//increment asynch count
	model.state.aSynchOperationCount += 1;
	//call dao
	//for some reason JQuery insists on overriding the this keyword. STUPID!!! So I have to pass a refference in event.data.commandInstance
	ServiceLocator.getInstance().dao.getUser( event.data.commandInstance );
}

LoginUserCommand.prototype.result = function ( event ){
	//console.log( event.result );
	var model = ModelLocator.getInstance();
	//model.testValue = event.payload.data;
	model.state.aSynchOperationCount -= 1;
	if( model.state.aSynchOperationCount == 0 ){
		model.state.aSynchOpComplete = true;
	}
	if( event.result != null && event.result instanceof User == true ){
		//get user from cache
		model.state.user = event.result;
		model.state.showFBLoginButton = false;
	}else if( Main.urlParams.access_token ){
		//see if there is a access_token from FB; this is sent in oAuth redirect and hence is a potential state
		model.state.user.fbToken = Main.urlParams.access_token;
		var currentTime = new Date();
		model.state.user.fbTokenExpiresOn = new Date( currentTime.valueOf()+ ( Main.urlParams.expires_in * 1000 ) );
		//dispatch event to get FB user profile and persist to API
		$(eventDispatcher).trigger( new SrfBetEvent( SrfBetEvent.GET_FBPROFILE, {data:model.state.user} ) );
	}else{
		//set login button state in model to visible
		model.state.showFBLoginButton = true;
	}
}

LoginUserCommand.prototype.fault = function ( event ){
	//console.log( event );
	var model = ModelLocator.getInstance();
	model.state.aSynchOperationCount -= 1;
	if( model.state.aSynchOperationCount == 0 ){
		model.state.aSynchOpComplete = true;
	}
	model.state.appErrorMessage = "An error occurred on the server. " + event.error;
	model.state.appError = true;
}
/*#################### End LoginUserCommand ####################*/